﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.IndicatorHistory
{
    public class HistoryEdit : PatientBasePage    
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected LabcheckInfo IndicatorData;
        protected IndicatorHistoryInfo IndicatorHistoryData;

        protected new void HandleCommon()
        {
            this.AddLocation("实验室指标记录", "/IndicatorHistory/Index.aspx");
            this.AddLocation("修改历史记录", "/IndicatorHistory/HistoryEdit.aspx");
            this.SetTitle("修改实验室指标历史记录");

            int id = RequestTools.GetInt("id", 0);
            if (id <= 0)
            {
                this.AddErrLine("无效的参数");
                return;
            }

            IndicatorHistoryData = PatientIndicatorBLL.Instance.GetIndicatorHistory(PrData.Id, id);
            if (IndicatorHistoryData.Id <= 0)
            {
                this.AddErrLine("无效的参数");
                return;
            }

            IndicatorData = LabcheckBLL.Instance.GetLabcheckInfo(IndicatorHistoryData.IndicatorId);
            if (IndicatorData.Id <= 0)
            {
                this.AddErrLine("无效的参数");
                return;
            }

            if (IndicatorData.IsQuantified > 0)
            {
                //解析正常值字符串
                string[] normalValues = IndicatorData.NormalValues.Split('@');
                if (normalValues.Length >= 2)
                {
                    IndicatorData.NormalMinValue = Convert.ToDouble(normalValues[0]);
                    IndicatorData.NormalMaxValue = Convert.ToDouble(normalValues[1]);
                }
            }
        }

        protected new void HandleGet()
        {
            this.AddDatePicker();
        }

        protected new void HandlePost()
        {
            IndicatorHistoryInfo info = this.ValidateForm();
            if (info == null) return;
            bool succ = PatientIndicatorBLL.Instance.UpdateIndicatorHistory(info);
            
            if (succ)
            {
                this.AddMsgLine("修改成功");
            }
            else
            {
                this.AddErrLine("修改失败");
            }

            this.SetUrl("/IndicatorHistory/List.aspx?trmid=" + IndicatorData.Id);
            this.SetMetaRefresh();
        }

        protected IndicatorHistoryInfo ValidateForm()
        {
            IndicatorHistoryInfo info = IndicatorHistoryData;

            //// 对时间进行判断
            //string value = "";
            //DateTime date;
            //bool succ = false;

            //if (TrmHistoryData.State == Constants.CURRENT_USING)
            //{
            //    info.UsePurpose = RequestTools.GetFormInt("usepurpose", -1);
            //}

            //if (TrmHistoryData.State == Constants.STOPED_USE)
            //{
            //    info.StopReason = RequestTools.GetFormInt("stopreason", -1);
            //}

            //if (TrmHistoryData.State == Constants.CURRENT_USING || TrmHistoryData.State == Constants.CHANGE_DOSAGE)
            //{
            //    info.DataValue = RequestTools.GetFormDouble("dosage", -1.0);
            //    if (info.DataValue < 0)
            //    {
            //        this.AddErrLine("请填写正确的剂量 ");
            //        return null;
            //    }
            //} 
            
            //value = RequestTools.GetFormString("startdate");
            //if (value == "")
            //{
            //    this.AddErrLine("请填写日期！");
            //    return null;
            //}

            //succ = DateTime.TryParse(value, out date);
            //if (succ == false)
            //{
            //    this.AddErrLine("错误的日期：" + value);
            //    return null;
            //}
            //info.StartDate = date;

            // //对时间进行检查
            //if (DateTime.Compare(date, DateTime.Today) > 0)
            //{
            //    this.AddErrLine("填写的日期 " + date.ToString("yyyy-MM-dd") + " 不能比当前日期 " + DateTime.Today.ToString("yyyy-MM-dd") + " 晚！");
            //    return null;
            //}
            //else if (DateTime.Compare(date, PrData.StartDate) < 0)
            //{
            //    this.AddErrLine("填写的日期 " + date.ToString("yyyy-MM-dd") + " 不能比第一次发病日期 " + PrData.StartDate.ToString("yyyy-MM-dd") + " 早！");
            //    return null;
            //}

            ////需要对判断是否有相同时间的记录（如果已经修改过时间）
            //if (TrmHistoryData.StartDate.CompareTo(info.StartDate) != 0 && PatientTreatmentBLL.Instance.CheckTreatmentHistory(PrData.Id, TrmData.Id, info.StartDate))
            //{
            //    //提示是否需要覆盖历史记录
            //    this.AddErrLine("是否需要覆盖" + value + "已有的历史记录？");
            //    return null;
            //}

            //info.UpdateDate = DateTime.Now;
            //info.TreatmentId = TrmData.Id;
            //info.PRId = PrData.Id;

            return info;
        }
    }
}